package com.bizteco.ishark;

import java.util.LinkedList;
import java.util.List;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.ListViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.PlatformUI;
import org.eclipse.swt.custom.TableTree;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;
import org.eclipse.swt.widgets.TreeColumn;

public class DriverDialog2 extends Dialog {
	private Text text;
	private Text text_2;
	private Text text_3;
	private Text text_4;
	private Text text_5;
	private Button bt_AddFile;
	public ListViewer listViewer;
	public java.util.LinkedList< DriverListVO> driverLists;
	
	/**
	 * Create the dialog.
	 * @param parentShell
	 */
	public DriverDialog2(Shell parentShell) {
		super(parentShell);
		setShellStyle(SWT.CLOSE | SWT.MIN | SWT.MAX | SWT.RESIZE);
		driverLists = new LinkedList< DriverListVO>();
	}

	/**
	 * Create contents of the dialog.
	 * @param parent
	 */
	@Override
	protected Control createDialogArea(Composite parent) {
		
		Composite container = (Composite) super.createDialogArea(parent);
		container.setLayout(new GridLayout(4, false));
		
		Label lblNewLabel = new Label(container, SWT.NONE);
		lblNewLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblNewLabel.setText("New Label");
		
		text = new Text(container, SWT.BORDER);
		text.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		Label lblNewLabel_1 = new Label(container, SWT.NONE);
		lblNewLabel_1.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblNewLabel_1.setText("New Label");
		
		Combo combo_1 = new Combo(container, SWT.NONE);
		combo_1.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		Label lblNewLabel_2 = new Label(container, SWT.NONE);
		lblNewLabel_2.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblNewLabel_2.setText("New Label");
		
		text_2 = new Text(container, SWT.BORDER);
		text_2.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 3, 1));
		
		Label lblNewLabel_3 = new Label(container, SWT.NONE);
		lblNewLabel_3.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblNewLabel_3.setText("New Label");
		
		text_3 = new Text(container, SWT.BORDER);
		text_3.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 3, 1));
		
		Label lblNewLabel_4 = new Label(container, SWT.NONE);
		lblNewLabel_4.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblNewLabel_4.setText("New Label");
		
		text_4 = new Text(container, SWT.BORDER);
		text_4.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 3, 1));
		
		Label lblNewLabel_5 = new Label(container, SWT.NONE);
		lblNewLabel_5.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblNewLabel_5.setText("New Label");
		
		text_5 = new Text(container, SWT.BORDER);
		text_5.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		new Label(container, SWT.NONE);
		new Label(container, SWT.NONE);
		
		TabFolder tabFolder = new TabFolder(container, SWT.NONE);
		GridData gd_tabFolder = new GridData(SWT.LEFT, SWT.CENTER, true, false, 4, 1);
		gd_tabFolder.widthHint = 452;
		gd_tabFolder.heightHint = 267;
		tabFolder.setLayoutData(gd_tabFolder);
		
		TabItem cItem_Libaries = new TabItem(tabFolder, SWT.NONE);
		cItem_Libaries.setText("Libaries");
		
		Composite composite = new Composite(tabFolder, SWT.NONE);
		cItem_Libaries.setControl(composite);
		composite.setLayout(new GridLayout(2, false));
		
		listViewer = new ListViewer(composite, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
		listViewer.setLabelProvider( new ListLabelProvider());
		listViewer.setContentProvider( new ListContentProvider());
		org.eclipse.swt.widgets.List list = listViewer.getList();
		GridData gd_list = new GridData(SWT.LEFT, SWT.CENTER, true, false, 1, 6);
		gd_list.widthHint = 336;
		list.setLayoutData(gd_list);
		
		bt_AddFile = new Button(composite, SWT.CENTER);
		bt_AddFile.addSelectionListener( new AddFile());
		bt_AddFile.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		bt_AddFile.setText("Add File");
		
		Button bt_AddFolder = new Button(composite, SWT.CENTER);
		bt_AddFolder.addSelectionListener( new AddDirectory());
		bt_AddFolder.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		bt_AddFolder.setText("Add Folder");
		
		Button bt_Delete = new Button(composite, SWT.NONE);
		bt_Delete.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1));
		bt_Delete.setText("Delete");
		
		Button bt_Up = new Button(composite, SWT.NONE);
		bt_Up.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		bt_Up.setText("Up");
		
		Button bt_Down = new Button(composite, SWT.NONE);
		bt_Down.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		bt_Down.setText("Down");
		
		Button bt_Classpath = new Button(composite, SWT.NONE);
		bt_Classpath.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		bt_Classpath.setText("Classpath");
		
		TabItem tabItem = new TabItem(tabFolder, SWT.NONE);
		tabItem.setText("New Item");
		
		TabItem cItem_Properties = new TabItem(tabFolder, SWT.NONE);
		cItem_Properties.setText("Connection Properties");
		
		Tree tree = new Tree(tabFolder, SWT.BORDER);
		cItem_Properties.setControl(tree);
		
		TreeColumn trclmnNewColumn = new TreeColumn(tree, SWT.NONE);
		trclmnNewColumn.setWidth(100);
		trclmnNewColumn.setText("ABC1");
		
		TabItem cItem_Prameters = new TabItem(tabFolder, SWT.NONE);
		cItem_Prameters.setText("Adv. parameters");

		return container;
	}

	/**
	 * Create contents of the button bar.
	 * @param parent
	 */
	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL,
				true);
		createButton(parent, IDialogConstants.CANCEL_ID,
				IDialogConstants.CANCEL_LABEL, false);
	}

	/**
	 * Return the initial size of the dialog.
	 */
	@Override
	protected Point getInitialSize() {
		return new Point(486, 440);
	}
	
	class AddFile implements SelectionListener {
	    
		public void widgetSelected(SelectionEvent event) {
			FileDialog fd = new FileDialog( Display.getDefault().getActiveShell(), SWT.OPEN);
			fd.setText("Open driver library");
			fd.setFilterPath("C:/");
			String[] filterExt = { "*.jar", "*.*" };
			fd.setFilterExtensions( filterExt);
			String selectedFileName = fd.open();
	      
			if( selectedFileName == null) // Cancel
				return;
	      
			listViewer.add( selectedFileName);
	    }

	    public void widgetDefaultSelected( SelectionEvent event) {}
	 
	}
	
	class AddDirectory implements SelectionListener {
	    
		public void widgetSelected(SelectionEvent event) {
			org.eclipse.swt.widgets.DirectoryDialog directoryDaialog = new org.eclipse.swt.widgets.DirectoryDialog( Display.getDefault().getActiveShell(), SWT.OPEN);
			directoryDaialog.setText("Open driver directory");
			directoryDaialog.setFilterPath("C:/");
			String selectedDirectoryName = directoryDaialog.open();
			if( selectedDirectoryName == null) // Cancel
				return;
			
			DriverListVO data = new DriverListVO();
			data.setName( selectedDirectoryName);
			data.setFile( false);
			driverLists.add( data);
			listViewer.setInput( driverLists);
			
//			Object[] objs = new Object[2];
//			objs[0] = img;
//			objs[1] = selectedDirectoryName;
//			listViewer.add( objs);
	    }

	    public void widgetDefaultSelected( SelectionEvent event) {}
	 
	}
	
	public class ListLabelProvider implements ILabelProvider {

	    @Override
	    public void dispose() {
	    }

	    @Override
	    public String getText(Object element) {
	    	return (( DriverListVO)element).getName();
	    }

	    @Override
	        public Image getImage(Object element) {
	    		return (( DriverListVO)element).getImage();
	    }

		@Override
		public void addListener(ILabelProviderListener listener) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public boolean isLabelProperty(Object element, String property) {
			// TODO Auto-generated method stub
			return false;
		}

		@Override
		public void removeListener(ILabelProviderListener listener) {
			// TODO Auto-generated method stub
			
		}
	}
	
	public class ListContentProvider implements IStructuredContentProvider {
		
		@Override
		public void dispose() {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public Object[] getElements(Object inputElement) {
			// TODO Auto-generated method stub
			LinkedList< DriverListVO> driverList = ( LinkedList< DriverListVO>)inputElement;
			return driverList.toArray();
		}

	}
	
}


